<template>
  <div class="orders">
    <h2>我的订单</h2>
    
    <el-tabs v-model="activeTab" @tab-click="handleTabClick">
      <el-tab-pane label="全部" name="ALL" />
      <el-tab-pane label="待付款" name="PENDING" />
      <el-tab-pane label="待发货" name="PAID" />
      <el-tab-pane label="待收货" name="SHIPPED" />
      <el-tab-pane label="已完成" name="COMPLETED" />
    </el-tabs>
    
    <div class="order-list">
      <div v-for="order in orders" :key="order.id" class="order-card">
        <div class="order-header">
          <span class="shop-name">{{ order.shop.name }}</span>
          <span class="order-status">{{ orderStatusText[order.status] }}</span>
        </div>
        
        <div class="order-items">
          <div v-for="item in order.items" :key="item.id" class="order-item">
            <el-image :src="item.product.imageUrl" :alt="item.product.name" />
            <div class="item-info">
              <h4>{{ item.product.name }}</h4>
              <p>数量: {{ item.quantity }}</p>
              <p class="price">¥{{ item.price }}</p>
            </div>
          </div>
        </div>
        
        <div class="order-footer">
          <div class="total">
            总计: <span class="total-price">¥{{ order.totalAmount }}</span>
          </div>
          <div class="actions">
            <el-button 
              v-if="order.status === 'PENDING'" 
              type="primary"
              @click="goPay(order.id)"
            >
              去支付
            </el-button>
            <el-button 
              v-if="order.status === 'SHIPPED'" 
              type="success"
              @click="confirmReceive(order.id)"
            >
              确认收货
            </el-button>
            <el-button 
              v-if="order.status === 'COMPLETED'" 
              @click="goReview(order.id)"
            >
              评价
            </el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useStore } from 'vuex'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'

const store = useStore()
const router = useRouter()
const activeTab = ref('ALL')
const orders = ref([])

const orderStatusText = {
  PENDING: '待付款',
  PAID: '待发货',
  SHIPPED: '待收货',
  COMPLETED: '已完成',
  CANCELLED: '已取消'
}

const loadOrders = async (status = 'ALL') => {
  try {
    const response = await store.dispatch('order/getOrders', { status })
    orders.value = response
  } catch (error) {
    ElMessage.error('加载订单列表失败')
  }
}

const handleTabClick = (tab) => {
  loadOrders(tab.props.name)
}

const goPay = (orderId) => {
  router.push(`/payment/${orderId}`)
}

const confirmReceive = async (orderId) => {
  try {
    await store.dispatch('order/updateOrderStatus', {
      orderId,
      status: 'COMPLETED'
    })
    ElMessage.success('确认收货成功')
    await loadOrders(activeTab.value)
  } catch (error) {
    ElMessage.error('确认收货失败')
  }
}

const goReview = (orderId) => {
  router.push(`/review/${orderId}`)
}

onMounted(() => {
  loadOrders()
})
</script>

<style scoped>
.orders {
  padding: 20px;
}

.order-card {
  margin: 20px 0;
  border: 1px solid #eee;
  border-radius: 4px;
}

.order-header {
  display: flex;
  justify-content: space-between;
  padding: 10px 20px;
  background-color: #f8f8f8;
}

.order-items {
  padding: 20px;
}

.order-item {
  display: flex;
  margin-bottom: 10px;
}

.order-item .el-image {
  width: 80px;
  height: 80px;
  margin-right: 20px;
}

.order-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  border-top: 1px solid #eee;
}

.total-price {
  color: #f56c6c;
  font-size: 18px;
  font-weight: bold;
}

.actions {
  display: flex;
  gap: 10px;
}
</style> 